package com.ruoyi.basics.controller;

import com.ruoyi.basics.domain.WmsLocation;
import com.ruoyi.basics.domain.vo.WmsLocationVo;
import com.ruoyi.basics.service.IWmsLocationService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 库位管理Controller
 * 
 * @author ruoyi
 * @date 2025-06-09
 */
@RestController
@RequestMapping("/basics/location")
public class WmsLocationController extends BaseController
{
    @Autowired
    private IWmsLocationService wmsLocationService;

    /**
     * 查询库位管理列表
     */
    @PreAuthorize("@ss.hasPermi('basics:location:list')")
    @GetMapping("/list")
    public TableDataInfo list(WmsLocation wmsLocation)
    {
        startPage();
        List<WmsLocationVo> list = wmsLocationService.selectWmsLocationList(wmsLocation);
        return getDataTable(list);
    }

    /**
     * 导出库位管理列表
     */
    @PreAuthorize("@ss.hasPermi('basics:location:export')")
    @Log(title = "库位管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsLocation wmsLocation)
    {
        List<WmsLocationVo> list = wmsLocationService.selectWmsLocationList(wmsLocation);
        ExcelUtil<WmsLocationVo> util = new ExcelUtil<WmsLocationVo>(WmsLocationVo.class);
        util.exportExcel(response, list, "库位管理数据");
    }

    /**
     * 获取库位管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('basics:location:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(wmsLocationService.selectWmsLocationById(id));
    }

    /**
     * 新增库位管理
     */
    @PreAuthorize("@ss.hasPermi('basics:location:add')")
    @Log(title = "库位管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated WmsLocation wmsLocation)
    {
        return toAjax(wmsLocationService.insertWmsLocation(wmsLocation));
    }

    /**
     * 修改库位管理
     */
    @PreAuthorize("@ss.hasPermi('basics:location:edit')")
    @Log(title = "库位管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmsLocation wmsLocation)
    {
        return toAjax(wmsLocationService.updateWmsLocation(wmsLocation));
    }

    /**
     * 删除库位管理
     */
    @PreAuthorize("@ss.hasPermi('basics:location:remove')")
    @Log(title = "库位管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(wmsLocationService.deleteWmsLocationByIds(ids));
    }


    /**
     * 库位状态修改
     */
    @PreAuthorize("@ss.hasPermi('basics:location:LocationStatus')")
    @Log(title = "库位状态修改", businessType = BusinessType.UPDATE)
    @PostMapping("/LocationStatus/{id}/{status}")
    public AjaxResult LocationStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        return toAjax(wmsLocationService.LocationStatus(id, status));
    }


    /**
     * 生成库位编码的接口
     * @return 新生成的库位编码
     */
    @PreAuthorize("@ss.hasPermi('basics:location:add')")

    @GetMapping("/generateLocationCode")
    public String generateLocationCode() {
        return wmsLocationService.generateLocationCode();
    }


    /**
     * 导入
     */
    @PreAuthorize("@ss.hasPermi('basics:location:import')")
    @Log(title = "导入", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult excelImport(MultipartFile file) throws Exception {
        ExcelUtil<WmsLocationVo> util = new ExcelUtil<WmsLocationVo>(WmsLocationVo.class);
        List<WmsLocationVo> wmsLocationVos = util.importExcel(file.getInputStream());
        return toAjax(wmsLocationService.insertWmsLocations(wmsLocationVos));
    }

}
